package member

import (
	"com.cyt.yunpay/app/model"
	"com.cyt.yunpay/app/service"
	"com.cyt.yunpay/app/service/pay"
	"com.cyt.yunpay/library/response"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
	"github.com/gogf/gf/util/gvalid"
)

// 会员API管理对象
var Member = new(memberApi)

type memberApi struct{}

// @summary 保存用户接口
func (a *memberApi) SaveMember(r *ghttp.Request) {
	var (
		setRealNameReq    *model.SetRealNameReq    // 实名认证请求验证
		relationMobileReq *model.RelationMobileReq // 关联手机号请求验证
	)

	step := gconv.Int(r.Get("step"));
	rule := "required|in:1,2,3"
	msgs := map[string]string{
		"required": "请选择保存节点",
		"in":       "保存节点选择错误",
	}
	if e := gvalid.Check(step, rule, msgs); e != nil {
		response.JsonExit(r, 422, e.String())
	}

	if step == 1 {
		data, err := service.AllInPayService.CreateMember(r);
		if err != nil {
			response.JsonExit(r, 400, err.Error())
		}
		response.JsonSuccess(r, "ok", data)
	} else if step == 2 {
		if err := r.ParseForm(&setRealNameReq); err != nil {
			response.JsonExit(r, 400, err.Error())
		}
		// 根据用户名查找用户信息
		memberInfo, err := service.Member.GetUserByUsername(setRealNameReq.Username)
		if err != nil {
			response.JsonExit(r, 400, err.Error())
		}

		if memberInfo == nil {
			uid, err := service.AllInPayService.CreateMember(r);
			if err != nil {
				response.JsonExit(r, 400, err.Error())
			}
			r.SetCtxVar("uid", uid)
		} else {
			r.SetCtxVar("uid", memberInfo.Uid)
		}

		if memberInfo != nil && memberInfo.IsRealAuth == 1 {
			response.JsonExit(r, 400, "已实名认证")
		}
		data, err := service.AllInPayService.RealNameAuth(r);
		if err != nil {
			response.JsonExit(r, 400, err.Error())
		}
		response.JsonExit(r, 0, "ok", data)
	} else if step == 3 {
		if err := r.ParseForm(&relationMobileReq); err != nil {
			response.JsonExit(r, 400, err.Error())
		}
		// 根据用户名查找用户信息
		memberInfo, err := service.Member.GetUserByUsername(relationMobileReq.Username)
		if err != nil {
			response.JsonExit(r, 400, err.Error())
		}

		if memberInfo == nil {
			uid, err := service.AllInPayService.CreateMember(r);
			if err != nil {
				response.JsonExit(r, 400, err.Error())
			}
			r.SetCtxVar("uid", uid)
		} else {
			r.SetCtxVar("uid", memberInfo.Uid)
		}
		// 判断是否绑定
		if memberInfo != nil && memberInfo.IsRealAuth != 1 {
			_, err := service.AllInPayService.RealNameAuth(r);
			if err != nil {
				response.JsonExit(r, 400, err.Error())
			}
		}

		// 判断是否绑定手机
		if memberInfo != nil && memberInfo.IsBindMobile != 1 {
			_, err := service.AllInPayService.RelationMobile(r);
			if err != nil {
				response.JsonExit(r, 400, err.Error())
			}
		}
		response.JsonExit(r, 0, "ok")
	}
}

// @summary 用户注册接口
func (a *memberApi) CreateMember(r *ghttp.Request) {
	data, err := service.AllInPayService.CreateMember(r);
	if err != nil {
		response.JsonExit(r, 400, err.Error())
	}
	response.JsonSuccess(r, "ok", data)
}

// @summary 实名认证
func (a *memberApi) RealNameAuth(r *ghttp.Request) {
	data, err := service.AllInPayService.RealNameAuth(r);
	if err != nil {
		response.JsonExit(r, 400, err.Error())
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 发送手机验证码
func (a *memberApi) SendCode(r *ghttp.Request) {
	var (
		apiReq *model.SendCodeReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":            apiReq.Username,
		"Phone":                apiReq.Mobile,
		"VerificationCodeType": apiReq.Way,
	}
	data, err := pay.CommonPay("allinpay", "MemberSendCode", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 关联手机
func (a *memberApi) RelationMobile(r *ghttp.Request) {
	data, err := service.AllInPayService.RelationMobile(r);
	if err != nil {
		response.JsonExit(r, 400, err.Error())
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 电子协议页面
func (a *memberApi) SignContractPage(r *ghttp.Request) {
	var (
		apiReq *model.SignContractPageReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId": apiReq.Username,
		"JumpUrl":   apiReq.JumpUrl,
		"BackUrl":   apiReq.BackUrl,
		"Source":    apiReq.Source,
	}
	data, err := pay.CommonPay("allinpay", "MemberSignContractPage", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 电子协议
func (a *memberApi) SignContract(r *ghttp.Request) {
	var (
		apiReq *model.SignContractPageReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId": apiReq.Username,
		"JumpUrl":   apiReq.JumpUrl,
		"BackUrl":   apiReq.BackUrl,
		"Source":    apiReq.Source,
	}
	data, err := pay.CommonPay("allinpay", "MemberSignContract", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 设置企业信息
func (a *memberApi) CompanySetInfo(r *ghttp.Request) {
	var (
		apiReq *model.CompanySetInfoReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":        apiReq.Username,
		"CompanyBasicInfo": apiReq.CompanyBasicInfo,
		"BackUrl":          apiReq.BackUrl,
		"IsAuth":           apiReq.IsAuth,
	}
	data, err := pay.CommonPay("allinpay", "MemberSetCompanyInfo", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 影印件采集
func (a *memberApi) UploadIdcardCollect(r *ghttp.Request) {
	var (
		apiReq *model.UploadIdcardCollectReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":                  apiReq.Username,
		"OcrComparisonResultBackUrl": apiReq.OcrComparisonResultBackUrl,
		"PicType":                    apiReq.PicType,
		"Picture":                    apiReq.Picture,
	}
	data, err := pay.CommonPay("allinpay", "MemberUploadIdcardCollect", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 企业信息审核结果通知
func (a *memberApi) CompanyVerifyResult(r *ghttp.Request) {
	var (
		apiReq *model.CompanyVerifyResultReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":  apiReq.Username,
		"Result":     apiReq.Result,
		"CheckTime":  apiReq.CheckTime,
		"Remark":     apiReq.Remark,
		"FailReason": apiReq.FailReason,
	}
	data, err := pay.CommonPay("allinpay", "MemberVerifyResult", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 获取会员信息
func (a *memberApi) MemberInfo(r *ghttp.Request) {
	var (
		apiReq *model.MemberInfoReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId": apiReq.Username,
	}
	data, err := pay.CommonPay("allinpay", "MemberGetMemberInfoForIndividual", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 查询卡 bin
func (a *memberApi) BankCardBin(r *ghttp.Request) {
	var (
		apiReq *model.BankCardBinReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"CardNo": apiReq.CardNo,
	}
	data, err := pay.CommonPay("allinpay", "MemberGetBankCardBin", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 申请绑定银行卡
func (a *memberApi) ApplyBindCard(r *ghttp.Request) {
	var (
		apiReq *model.ApplyBindCardReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":    apiReq.Username,
		"CardNo":       apiReq.CardNo,
		"Phone":        apiReq.Mobile,
		"Name":         apiReq.RealName,
		"CardCheck":    apiReq.CardCheck,
		"IdentityType": apiReq.IdentityType,
		"IdentityNo":   apiReq.IdentityNo,
		"Validate":     apiReq.Validate,
		"Cvv2":         apiReq.Cvv2,
		"IsSafeCard":   apiReq.IsSafeCard,
		"UnionBank":    apiReq.UnionBank,
	}
	data, err := pay.CommonPay("allinpay", "MemberApplyBindBankCard", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 绑定银行卡
func (a *memberApi) BindBankCard(r *ghttp.Request) {
	var (
		apiReq *model.BindBankCardReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":        apiReq.Username,
		"TranceNum":        apiReq.TranceNum,
		"TransDate":        apiReq.TransDate,
		"Phone":            apiReq.Mobile,
		"Validate":         apiReq.Validate,
		"Cvv2":             apiReq.Cvv2,
		"VerificationCode": apiReq.VerificationCode,
	}
	data, err := pay.CommonPay("allinpay", "MemberBindBankCard", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 解绑银行卡
func (a *memberApi) UnBindBankCard(r *ghttp.Request) {
	var (
		apiReq *model.QueryBankCardReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId": apiReq.Username,
		"CardNo":    apiReq.CardNo,
	}
	data, err := pay.CommonPay("allinpay", "MemberUnbindBankCard", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 查询银行卡
func (a *memberApi) QueryBankCard(r *ghttp.Request) {
	var (
		apiReq *model.QueryBankCardReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId": apiReq.Username,
		"CardNo":    apiReq.CardNo,
	}
	data, err := pay.CommonPay("allinpay", "MemberQueryBankCard", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 锁定会员
func (a *memberApi) MemberLock(r *ghttp.Request) {
	var (
		apiReq *model.MemberLockReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId": apiReq.Username,
	}
	if apiReq.IsLock == false { // 解锁
		_, err := pay.CommonPay("allinpay", "MemberUnlockMember", params)
		if err != nil {
			response.JsonExit(r, 1, gconv.String(err["message"]))
		}
	} else if apiReq.IsLock == true { // 锁定
		_, err := pay.CommonPay("allinpay", "MemberLockMember", params)
		if err != nil {
			response.JsonExit(r, 1, gconv.String(err["message"]))
		}
	}
	response.JsonExit(r, 0, "ok")
}

// @summary 会员绑定支付账户用户标识
func (a *memberApi) ApplyBindAcct(r *ghttp.Request) {
	var (
		apiReq *model.ApplyBindAcctReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":     apiReq.Username,
		"OperationType": apiReq.OperationType,
		"AcctType":      apiReq.AcctType,
		"Acct":          apiReq.Acct,
	}
	data, err := pay.CommonPay("allinpay", "MemberApplyBindAcct", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 修改绑定手机(银行卡验证)
func (a *memberApi) ChangeBankcardMobile(r *ghttp.Request) {
	var (
		apiReq *model.ChangeBankcardMobileReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":    apiReq.Username,
		"CardNo":       apiReq.CardNo,
		"Phone":        apiReq.Mobile,
		"Name":         apiReq.RealName,
		"CardCheck":    apiReq.CardCheck,
		"IdentityType": apiReq.IdentityType,
		"IdentityNo":   apiReq.IdentityNo,
		"Validate":     apiReq.Validate,
		"Cvv2":         apiReq.Cvv2,
	}
	data, err := pay.CommonPay("allinpay", "MemberBankCardChangeBindPhone", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 修改绑定手机(银行卡验证)
func (a *memberApi) ChangeBankcardVerify(r *ghttp.Request) {
	var (
		apiReq *model.ChangeBankcardVerifyReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":        apiReq.Username,
		"TranceNum":        apiReq.TranceNum,
		"Phone":            apiReq.Mobile,
		"VerificationCode": apiReq.VerificationCode,
		"Validate":         apiReq.Validate,
		"Cvv2":             apiReq.Cvv2,
	}
	data, err := pay.CommonPay("allinpay", "MemberVerifyBankCardChangeBindPhone", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 会员子账号开通
func (a *memberApi) CreateBankSubAcctNo(r *ghttp.Request) {
	var (
		apiReq *model.CreateBankSubAcctNoReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":    apiReq.Username,
		"AccountSetNo": apiReq.AccountSetNo,
		"AcctOrgType":  apiReq.AcctOrgType,
	}
	data, err := pay.CommonPay("allinpay", "MemberCreateBankSubAcctNo", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 设置支付密码【密码验证版】
func (a *memberApi) SetPayPwd(r *ghttp.Request) {
	var (
		apiReq *model.PayPwdReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":    apiReq.Username,
		"JumpPageType": apiReq.JumpPageType,
		"Phone":        apiReq.Mobile,
		"Name":         apiReq.RealName,
		"IdentityType": apiReq.IdentityType,
		"IdentityNo":   apiReq.IdentityNo,
		"JumpUrl":      apiReq.JumpUrl,
		"ErrorJumpUrl": apiReq.ErrorJumpUrl,
		"BackUrl":      apiReq.BackUrl,
	}
	data, err := pay.CommonPay("allinpay", "MemberSetPayPwd", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 修改支付密码【密码验证版】
func (a *memberApi) UpdatePayPwd(r *ghttp.Request) {
	var (
		apiReq *model.PayPwdReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":    apiReq.Username,
		"JumpPageType": apiReq.JumpPageType,
		"Phone":        apiReq.Mobile,
		"Name":         apiReq.RealName,
		"IdentityType": apiReq.IdentityType,
		"IdentityNo":   apiReq.IdentityNo,
		"JumpUrl":      apiReq.JumpUrl,
		"ErrorJumpUrl": apiReq.ErrorJumpUrl,
		"BackUrl":      apiReq.BackUrl,
	}
	data, err := pay.CommonPay("allinpay", "MemberUpdatePayPwd", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 重置支付密码【密码验证版】
func (a *memberApi) ResetPayPwd(r *ghttp.Request) {
	var (
		apiReq *model.PayPwdReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":    apiReq.Username,
		"JumpPageType": apiReq.JumpPageType,
		"Phone":        apiReq.Mobile,
		"Name":         apiReq.RealName,
		"IdentityType": apiReq.IdentityType,
		"IdentityNo":   apiReq.IdentityNo,
		"JumpUrl":      apiReq.JumpUrl,
		"ErrorJumpUrl": apiReq.ErrorJumpUrl,
		"BackUrl":      apiReq.BackUrl,
	}
	data, err := pay.CommonPay("allinpay", "MemberResetPayPwd", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}

// @summary 修改绑定手机【密码验证版】
func (a *memberApi) UpdatePhoneByPayPwd(r *ghttp.Request) {
	var (
		apiReq *model.PayPwdReq
	)
	if err := r.ParseForm(&apiReq); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	params := map[string]interface{}{
		"BizUserId":    apiReq.Username,
		"JumpPageType": apiReq.JumpPageType,
		"Phone":        apiReq.Mobile,
		"Name":         apiReq.RealName,
		"IdentityType": apiReq.IdentityType,
		"IdentityNo":   apiReq.IdentityNo,
		"JumpUrl":      apiReq.JumpUrl,
		"ErrorJumpUrl": apiReq.ErrorJumpUrl,
		"BackUrl":      apiReq.BackUrl,
	}
	data, err := pay.CommonPay("allinpay", "MemberUpdatePhoneByPayPwd", params)
	if err != nil {
		response.JsonExit(r, 1, gconv.String(err["message"]))
	}
	response.JsonExit(r, 0, "ok", data)
}
